<?php

namespace falcon5\inc\WPProviders;

use falcon5\inc\Base\WPOption;
use falcon5\inc\Base\ServiceProviderInterface;
use falcon5\inc\Helpers\AssetsHelper;

class LanguageSwitcherShortcode implements ServiceProviderInterface
{

    public function register()
    {
        add_shortcode('Falcon5', [$this, 'render_shortcode']);
        add_shortcode('falcon5', [$this, 'render_shortcode']);
        add_shortcode('fc5-link', [$this, 'render_single_item']);
    }

    public function render_shortcode($atts)
    {
        $uniqueId = wp_rand(10000000, 88888888);
        return '<div class="falcon5_shortcode_wrapper" id="fc5-wrapper-' . $uniqueId . '"></div>';
    }


    public function render_single_item($atts)
    {
        $langCode = $atts['lang'];
        $langLabel = $atts['label'];
        /** @var WPOption $falcon5Option */
        $falcon5Option = falcon5_app(WPOption::class);
        $flagSize = $falcon5Option->getFlagSize();
        $flagStyle = $falcon5Option->getFlagStyle();
        $styleClass = 'fc5-link';
        $styleClass .= ' flag-size-' . $flagSize;
        if ($flagStyle === '3d') {
            $styleClass .= ' flag-style-3d';
        }
        $html =  '<a href="#" data-fc5-lang="' . esc_attr($langCode) . '" class="' . $styleClass . '">';
        $html .= '<img src="' . $this->getFlagImg($langCode) . '" width="' . esc_attr($flagSize) . '" height="' . esc_attr($flagSize) . '" alt="' . esc_attr($langCode) . '">';
        $html .= '</a>';
        return $html;
    }

    private function getFlagImg($langCode)
    {
        $assets = esc_url( AssetsHelper::getAssetUri() );
        return $assets . '/images/flags/' . $langCode . '.svg';
    }
}